<?php

namespace App\Admin\Controllers;

use App\Admin\Exports\UserProfileExport;
use App\Admin\Extensions\UserCoinOp;
use App\Admin\Extensions\UserOp;
use App\Http\Controllers\Controller;
use App\Model\UserCoin;
use App\Model\UserProfile;
use App\Model\User;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;
use App\Admin\Exports\UserCoinExport;

class UserCoinController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('用户虚拟账户');
            $content->description('');
            $content->body($this->grid());
        });
    }

    /**
     * 编辑
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('虚拟账户编辑');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('创建虚拟账户');
            $content->description('');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        $grid = new Grid(new UserCoin());

            $grid->model()->where('user_id','>',100)->orderBy('user_id', 'desc');
            $grid->userProfile()->user_profile_id('用户ID');
            $grid->userProfile()->user_id('内部ID');
            $grid->userProfile()->nick_name('昵称');
            $grid->userProfile()->created_at('注册时间')->display(function ($time) {
                return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
            });
            $grid->userProfile()->updated_at('最后活跃时间')->display(function ($time) {
                return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
            });
            $grid->userItem()->device_id('设备ID');
            // $grid->userItem()->phone('手机号');
            $grid->userProfile()->gender('昵称')->display(function ($gender) {
                return $gender == 1 ? '男' : ($gender == 0 ? '女' : '-');
            });
            $grid->userProfile()->role('用户角色')->display(function ($role) {
                return $role == UserProfile::ROLE_HOST ? '女用户' : '普通用户';
            });
            $grid->recharge_coin('充值账户余额')->display(function ($recharge_coin) {
                return (int)$recharge_coin/10;
            });
            $grid->income_coin('收入账户余额'); //->editable()
            $grid->total_coin('账户总余额')->display(function () {
                return $this->recharge_coin + $this->income_coin;
            });
            $grid->freeze_coin('当前冻结额度')->sortable();
            $grid->total_income_coin('总计收入金额');
            //禁用导出
            $grid->exporter(new UserCoinExport($grid));
            //禁用行选择
            $grid->disableRowSelector();

            $grid->filter(function ($filter) {
                $filter->expand();
                $filter->disableIdFilter();
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('userProfile.user_profile_id', '用户ID');
                });
                $filter->column(1 / 3, function ($filter) {
                    $filter->between('recharge_coin', '用户充值余额');
                });
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('userItem.device_id','用户设备ID');
                });
            });
            // $grid->disableActions();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableView();
                $actions->disableEdit();

                $actions->append(new UserCoinOp($actions->getKey(), $actions->row, 'usercoin/op'));
            });
        return $grid;
    }

    function verify()
    {
        $id = request('id');
        $action = intval(request('action'));
        $record = UserCoin::find($id);
        if (!$record) return ['ret' => -1, 'message' => '记录不存在'];
        switch ($action) {
            case 'clear_freeze':
                // UserCoin::costCoin($record->user_id, $record->freeze_coin, UserCoin::getUserInfo($record->user_id));
                UserCoin::clearFreezeCoin($record->user_id);
                break;
        }
        return ['ret' => 0, 'message' => '操作成功！'];
    }
}
